package com.ncp.servlet.index;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

import com.ncp.model.biz.index.IntroManager;
import com.ncp.model.biz.news.NewsManager;


public class IntroClServlet extends HttpServlet {

	public IntroClServlet() {
	super();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	   throws ServletException, IOException {
		boolean b=false;
		String s[]=new String[2];
		//设置request编码，主要是为了处理普通输入框中的中文问题
		request.setCharacterEncoding("UTF-8");
		//这里对request进行封装，RequestContext提供了对request多个访问方法
		RequestContext requestContext = new ServletRequestContext(request);
		//判断表单是否是Multipart类型的。这里可以直接对request进行判断，不过已经以前的用法了
		
		String path1 = request.getServerName();
		String path2 = request.getRealPath(path1);
		int n=path2.lastIndexOf("\\");
		String S_realPath=path2.substring(0,n);
		String realPath =S_realPath+"\\"; 
		if(FileUpload.isMultipartContent(requestContext)){
		   DiskFileItemFactory factory = new DiskFileItemFactory();
		   //设置文件的缓存路径
		   factory.setRepository(new File("D:/image/"));
		   ServletFileUpload upload = new ServletFileUpload(factory);
		   //设置上传文件大小的上限，-1表示无上限 
		   upload.setSizeMax(-1);
		   List items = new ArrayList();
		   try {
		       //上传文件，并解析出所有的表单字段，包括普通字段和文件字段
			   items = upload.parseRequest(request);
		    } catch (FileUploadException e1) {
			   System.out.println("文件上传发生错误" + e1.getMessage());
		    }
		    //下面对每个字段进行处理，分普通字段和文件字段
		   Iterator it = items.iterator();
		   int i=-1;
		   while(it.hasNext()){
			   i++;
			   FileItem fileItem = (FileItem) it.next();
			   //如果是普通字段
			   if(fileItem.isFormField()){   
		    	s[i]=new String(fileItem.getString().getBytes("iso8859-1"), "UTF-8");
			   }else{
		    	 if(fileItem.getName().toString().equals("")){
		    		 s[i]="null";
		    	 }else{
		    		 String tempStr1[]=NewsManager.split(fileItem.getName(),"\\");
			    	 s[i]=tempStr1[tempStr1.length-1];
			    	 String tempStr2[]=NewsManager.split(s[i],".");
			    	 s[i]=UUID.randomUUID().toString()+"."+tempStr2[tempStr2.length-1];
				     //保存文件，其实就是把缓存里的数据写到目标路径下
				     if(fileItem.getName()!=null && fileItem.getSize()!=0){
					     File fullFile = new File(fileItem.getName());
					     //System.out.println(realPath);
					     File newFile = new File(realPath+"/images/index/" + s[i]);
					     try {
					    	 fileItem.write(newFile);
					     } catch (Exception e) {
					    	 e.printStackTrace();
					     }
				      }else{
				    	 System.out.println("文件没有选择 或 文件内容为空");
				     }
		    	 }
			 }
		
		   }
		}
		b=new IntroManager().updateIntro(s[1],s[0]);
		if(b){
			request.setAttribute("url", "pages/admin/IntroCl.jsp");
			request.getRequestDispatcher("pages/admin/operateOK.jsp").forward(request, response);
		}
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		this.doPost(request, response);
	}

}
